<template>
	<view class="modifyAddress">
		<uninavbar title="修改地址" leftIcon='leftIcon' rightIcon='' rightText="" />
		<view class="AddressTip">
			<image src="/static/images/productDetails/gt.png" mode="widthFix"></image>
			<view class="">
				<text>修改后会影响物流时效，送货上门等服务，只能原价修改。</text> 若因商品换仓，已发货，运费变更等原因导致修改失败，请您谅解
			</view>

		</view>
		<view class="originalAddress">
			<view>原地址:</view>
			<view class="originaText">
				{{orderDetailData.receiverProvince}}
				{{orderDetailData.receiverCity}}
				{{orderDetailData.receiverRegion}}
				{{orderDetailData.receiverDetailAddress}}
			</view>
			<view class="originaName">
				收件人：{{orderDetailData.receiverName}} {{orderDetailData.receiverPhone}}
			</view>

		</view>
		<view class="recentlyAddress">
			<view class="newAddress">
				<view class="">
					选择新的收货地址
				</view>
				<navigator class="item" url="/pages/user/newAddress">
					<view class="newAddressUrl">
						添加新地址
					</view>
				</navigator>
			</view>

			<view class="recent">
				近期使用：
			</view>
			<view class="addressList" v-for="(item,index) in addressListData">
				<view class="addressLeft">
					{{item.name[0]}}
				</view>
				<view class="addressRight">
					<view class="addressText">
						{{item.province}}{{item.city}}{{item.region}}{{item.detailAddress}}
					</view>
					<view class="">
						<text class="phone">{{item.name}} {{item.phoneNumber}}</text>
					</view>
				
					<view class="selectAddress" @click.stop="selectedDom(item,index)">
						<image v-if="item.show" src="/static/images/productDetails/show.png" mode=""></image>
						<view v-else class="selecteHide"></view>
					</view>
				</view>

			</view>
		</view>
		<view class="modifyFlex" @tap="popupTap()">
			<view class="modifyBtn">
				提交修改
			</view>
		</view>
		<uni-popup class="logisticsPopup" :show="popupShow" @close="popupShow = false" ref="popup" type="center">
			<view class="cancel">
				<view class="cancelTitle">
					是否修改地址
				</view>
				<view class="cancelBtn">
					<view class="" @tap="popupShow = !popupShow">
						取消
					</view>
					<view class="cancelBtnOk" @tap="orderUpdateAddress()">
						确定
					</view>
				</view>

			</view>
		</uni-popup>
	</view>

</template>

<script>
	import uninavbar from '@/components/uni-nav-bar/uni-nav-bar.vue';
	import API from '@/config/newApi.js'
	export default {
		components: {
			uninavbar,
		},
		data() {
			return {
				addressListData: [],
				goUrltype: '',
				orderId: '',
				addressId: '',
				popupShow: true,
				orderDetailData: {
					receiverCity: "",
					receiverDetailAddress: "",
					receiverName: "",
					receiverPhone: "",
					receiverProvince: "",
					receiverRegion: "",
				}
			}
		},
		onShow() {
			this.addressList()
		},
		onLoad(data) {
			this.orderId = data.orderId
			// this.addressList()
			this.orderDetail()
		},
		methods: {
			popupTap() {
				this.popupShow = !this.popupShow
			},
			orderDetail() {
				API.orderDetail(this.orderId).then(res => {
						if (res.code == 200) {
							this.orderDetailData = res.data
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			orderUpdateAddress() {
				let data = {
					orderId: this.orderId,
					addressId: this.addressId
				}

				API.orderUpdateAddress(data).then(res => {
						if (res.code == 200) {
							this.popupShow = !this.popupShow
							this.$alert(res.message);
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								});
							}, 1000); // 延迟2000毫秒（2秒）后执行
							
						}
					})
					.catch(err => {
						this.$alert(res.message);
					})
			},
			addressList() {
				API.addressList().then(res => {
						if (res.code == 200) {

							for (let item of res.data) {
								item['show'] = false
								if (item.defaultStatus == 1) {
									this.addressId = item.id
								}
							}
							this.addressListData = res.data
						}
					})
					.catch(err => {
						this.$alert(res.message);
					})
			},
			selectedDom(item, index) {
				for (let i of this.addressListData) {
					i.show = false
				}
				this.addressId = item.id
				this.addressListData[index].show = true
				// this.addressUpdate(item)
				this.orderDetailData = item
				this.orderDetailData = {
					receiverCity: item.city,
					receiverDetailAddress: item.detailAddress,
					receiverName: item.name,
					receiverPhone: item.phoneNumber,
					receiverProvince: item.province,
					receiverRegion: item.region,
				}
				console.log(item)
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background: #F9F9F9;
	}

	.modifyAddress {

		padding: 40rpx;

		.AddressTip {
			background-color: #fff;
			padding: 20rpx;
			display: flex;
			align-items: center;
			font-size: 24rpx;
			border-radius: 16rpx;
			margin-top: 80rpx;

			text {
				color: #6954D5;
			}

			image {
				width: 140rpx;
				margin-right: 20rpx;
			}
		}

		.originalAddress {
			margin: 20rpx 0;

			.originaText {
				font-size: 24rpx;
			}

			.originaName {
				font-size: 20rpx;
				color: #666;
			}
		}

		.recentlyAddress {
			border-radius: 16rpx;
			background-color: #fff;
			padding: 20rpx;
			height: calc(100vh - 680rpx);
			overflow: auto;

			.recent {
				font-size: 20rpx;
				margin-bottom: 20rpx;
				margin-top: 10rpx;
				color: #999;
			}

			.newAddress {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.newAddressUrl {
					font-size: 24rpx;
				}
			}

			.addressList {
				display: flex;
				align-items: flex-start;
				padding: 20rpx 0;
				border-bottom: 2rpx solid #f4f4f4;
				.addressLeft {
					width: 70rpx;
					height: 70rpx;
					background: #F3F2FC;
					color: #6954D5;
					text-align: center;
					line-height: 70rpx;
					border-radius: 50%;
					margin-right: 20rpx;

				}

				.addressRight {
					position: relative;
					flex: 1;

					.phone {
						color: #999;
						font-size: 20rpx;
					}

					.addressText {
						font-size: 26rpx;
						width: 80%;
					}

					.selectAddress {
						position: absolute;
						right: 0;
						top: 40rpx;

						image {
							width: 36rpx;
							height: 36rpx;
						}

						.selecteHide {
							width: 30rpx;
							height: 30rpx;
							border: 2rpx solid #999;
							border-radius: 50%;
						}
					}
				}
			}
		}
	}

	.modifyFlex {
		position: fixed;
		bottom: 50rpx;
		left: 0;
		width: 100%;

		.modifyBtn {
			width: 520rpx;
			height: 80rpx;
			line-height: 80rpx;
			background: #6954D5;
			border-radius: 20px;
			color: #fff;
			text-align: center;
			margin: auto;

		}
	}

	.cancelBtn {
		display: flex;
		align-items: center;

		view {
			width: 175rpx;
			height: 65rpx;
			background: #DDDDDD;
			border-radius: 23px;
			color: #fff;
			line-height: 65rpx;
			margin: 40rpx 20rpx;
			font-size: 24rpx;
			text-align: center;
		}

		.cancelBtnOk {
			background-color: #6954D5;
		}
	}

	.cancelTitle {
		margin-bottom: 8rpx;
		text-align: center;

	}
</style>